package org.example.struct.array2;

import com.alibaba.fastjson.JSON;

/**
 * @author liyishan
 * @date 2023/11/8 09:54
 * @apiNote
 */

public class Test01109CorpFlightBookings {

    public static void main(String[] args) {
        int[][] bookings = {{1, 2, 10}, {2, 3, 20}, {2, 5, 25}};
        int[] res = test2(bookings, 5);
        System.out.println(JSON.toJSON(res));
    }
    public static int[] test2(int[][] bookings, int n) {
        int[] nums = new int[n];
        for (int[] booking : bookings) {
            nums[booking[0] - 1] += booking[2];
            if (booking[1] < n) {
                nums[booking[1]] -= booking[2];
            }
        }
        for (int i = 1; i < n; i++) {
            nums[i] += nums[i - 1];
        }
        return nums;
    }

    public static int[] test(int[][] bookings, int n) {
        int[] res = new int[n];
        int m = bookings.length;
        for (int i = 0; i < m; i++) {
            int[] book = bookings[i];
            int start = book[0];
            int end = book[1];
            int num = book[2];
            for (int j = start - 1; j < end; j++) {
                res[j] += num;
            }
        }
        return res;
    }

}
